package com.dmwork.service.customer.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmwork.common.base.result.ExceptionUtils;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.result.ResultCodeEnum;
import com.dmwork.common.base.util.JwtInfo;
import com.dmwork.common.base.util.JwtUtils;
import com.dmwork.service.base.exception.DmworkException;
import com.dmwork.service.customer.entity.Customer;
import com.dmwork.service.customer.entity.vo.CustomerBriefInfoVo;
import com.dmwork.service.customer.entity.vo.CustomerDetailInfoVo;
import com.dmwork.service.customer.service.CustomerFollowerService;
import com.dmwork.service.customer.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Transactional
@Api(description = "用户服务，全局业务部分")
@RestController
@RequestMapping("/front/dm_work/customer")
public class FrontCustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerFollowerService customerFollowerService;

    @ApiOperation("获取指定用户信息")
    @GetMapping("get_info/{customerId}")
    public R getInfo(HttpServletRequest request, @ApiParam(value = "用户id", required = true) @PathVariable("customerId") String customerId) {
        CustomerDetailInfoVo customerDetailInfoVo = customerService.getDetailInfoById(customerId);

//        customerDetailInfoVo.setBackground(customerService.getSpaceDefaultBackgroundUrl());

        //判断当前用户是否已登录，若已登录则获取其关注的所有用户的id
        JwtInfo jwtToken = null;
        try {
            jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
        } catch (Exception e) {
            ExceptionUtils.getMessage(e);
        }
        List<String> followedCustomerIdList = null;
        if (jwtToken != null) {
            followedCustomerIdList = customerFollowerService.getFollowedCustomerIdList(jwtToken.getId());
            if (followedCustomerIdList != null && followedCustomerIdList.size() != 0){
                for (String id : followedCustomerIdList) {
                    //若该用户id在当前用户的关注中，则设置followed=true
                    if (customerDetailInfoVo.getId().equals(id)) {
                        customerDetailInfoVo.setFollowed(true);
                        break;
                    }
                }
            }
        }
        return R.ok().message("获取成功").data("item", customerDetailInfoVo);
    }

    @GetMapping("get_list/{page}/{limit}")
    public List<Customer> getList(@PathVariable Integer page, @PathVariable Integer limit) {
        List<Customer> customerList = customerService.getCustomerList(page, limit);
        return customerList;
    }

    @ApiOperation("根据用户id批量获取用户粗略信息")
    @PostMapping("get_brief_info_list")
    public R getBriefInfoList(HttpServletRequest request, @RequestBody List<String> customerIdList) {
        List<Customer> customerList = customerService.listByIds(customerIdList);
        //判断当前用户是否已登录，若已登录则获取其关注的所有用户的id
        JwtInfo jwtToken = null;
        try {
            jwtToken = JwtUtils.getCustomerInfoByJwtToken(request);
        } catch (Exception e) {
            ExceptionUtils.getMessage(e);
        }
        List<String> followedCustomerIdList = null;
        if (jwtToken != null) {
            followedCustomerIdList = customerFollowerService.getFollowedCustomerIdList(jwtToken.getId());
        }
        List<CustomerBriefInfoVo> customerBriefInfoVoList = new ArrayList<>();
        for (Customer customer : customerList) {
            CustomerBriefInfoVo customerBriefInfoVo = new CustomerBriefInfoVo();
            customerBriefInfoVo.setCustomerId(customer.getId());
            customerBriefInfoVo.setName(customer.getName());
            customerBriefInfoVo.setAvatar(customer.getAvatar());
            customerBriefInfoVo.setSign(customer.getSign());
            //若关注的id列表不为空，则判断该用户是否为已关注用户
            if (followedCustomerIdList != null && followedCustomerIdList.size() != 0) {
                for (String id : followedCustomerIdList) {
                    //若该用户id在当前用户的关注中，则设置followed=true
                    if (customerBriefInfoVo.getCustomerId().equals(id)) {
                        customerBriefInfoVo.setFollowed(true);
                        break;
                    }
                }
            }
            customerBriefInfoVoList.add(customerBriefInfoVo);
        }
        return R.ok().data("items", customerBriefInfoVoList);
    }

    @ApiOperation("检查用户名是否重复")
    @GetMapping("check_name_duplicate/{name}")
    public R checkNameDuplicate(@PathVariable String name) {
        int count = customerService.count(new QueryWrapper<Customer>().eq("name", name));
        if (count>0) {
            throw new DmworkException(ResultCodeEnum.REGISTER_NAME_ERROR);
        }
        return R.ok().message("该用户名未被注册");
    }

}
